package com.scu.dao;


import com.scu.utils.JDBCUtil;
import com.scu.entity.Post;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class PostDao {
    //添加一个用户
    public int insert(Post post) throws SQLException {
        //1. 连接
        Connection conn = JDBCUtil.getConnection();

        //2. sql
        String sql = "INSERT INTO post1(post_id,post_content,user_id,is_illegal,date,type,post_title) VALUES(?,?,?,?,?,?,?)";
        PreparedStatement prep = conn.prepareStatement(sql);
        prep.setInt(1,post.getPost_id());
        prep.setString(2,post.getPost_content());
        prep.setInt(3,post.getUser_id());
        prep.setInt(4,post.getIs_illegal());
        prep.setString(5,post.getDate());
        prep.setString(6,post.getType());
        prep.setString(7,post.getPost_title());

        //3. 结果
        int row = prep.executeUpdate();

        //4. 关闭连接
        conn.close();

        return row;
    }

    //删除
    public int delete(int post_id) throws SQLException {
        //1. 连接
        Connection conn = JDBCUtil.getConnection();

        //2. sql
        String sql = "delete from post1 where post_id=?";
        PreparedStatement prep = conn.prepareStatement(sql);
        prep.setInt(1,post_id);

        //3. 结果
        int row = prep.executeUpdate();

        //4. 关闭连接
        conn.close();

        return row;
    }

    public List<Post> findAll() throws SQLException {
        //1. 连接
        Connection conn = JDBCUtil.getConnection();

        //2. sql
        String sql = "select * from post1";
        PreparedStatement prep = conn.prepareStatement(sql);

        //3. 结果
        ResultSet rs = prep.executeQuery();
        //处理结果
        List<Post> list = new ArrayList<Post>();
        while(rs.next()){
            //rs代表的就是每一行
            Post post = new Post();
            //根据rs获取每一列的值
            int post_id = rs.getInt("post_id");
            String post_content = rs.getString("post_content");
            int user_id = rs.getInt("user_id");
            int is_illegal=rs.getInt("is_illegal");
            String date = rs.getString("date");
            String type = rs.getString("type");
            String post_title = rs.getString("post_title");

            //把该行每一列的值赋值给user对象
            post.setPost_id(post_id);
            post.setPost_content(post_content);
            post.setUser_id(user_id);post.setUser_id(user_id);
            post.setIs_illegal(is_illegal);
            post.setDate(date);
            post.setType(type);
            post.setPost_title(post_title);

            list.add(post);
        }

        //4. 关闭连接
        conn.close();

        return list;
    }
    public static List<Post> findillegal() throws SQLException {
        //1. 连接
        Connection conn = JDBCUtil.getConnection();

        //2. sql
        String sql = "select * from post1 where is_illegal = 1;";
        PreparedStatement prep = conn.prepareStatement(sql);

        //3. 结果
        ResultSet rs = prep.executeQuery();
        //处理结果
        List<Post> list = new ArrayList<Post>();
        while(rs.next()){
            //rs代表的就是每一行
            Post post = new Post();

            int id = rs.getInt("post_id");
            String post_content = rs.getString("post_content");
            int user_id = rs.getInt("user_id");
            int is_illegal = rs.getInt("is_illegal");
            String date = rs.getString("date");
            String type = rs.getString("type");
            String post_title = rs.getString("post_title");

            post.setPost_id(id);
            post.setPost_content(post_content);
            post.setUser_id(user_id);
            post.setIs_illegal(is_illegal);
            post.setDate(date);
            post.setType(type);
            post.setPost_title(post_title);







            list.add(post);
        }

        //4. 关闭连接
        conn.close();
        System.out.println(list);
        return list;
    }

    public static void main(String[] args) throws SQLException {
        List<Post> list = new PostDao().findAll();
        System.out.println(list);
    }
}



